#!/bin/bash

# postinst script for system-zones
#
# see: dh_installdeb(1)

#set -e

# summary of how this script can be called:
#        * <postinst> \`configure' <most-recently-configured-version>
#        * <old-postinst> \`abort-upgrade' <new version>
#        * <conflictor's-postinst> \`abort-remove' \`in-favour' <package>
#          <new-version>
#        * <postinst> \`abort-remove'
#        * <deconfigured's-postinst> \`abort-deconfigure' \`in-favour'
#          <failed-install-package> <version> \`removing'
#          <conflicting-package> <version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package

PATH=/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin
export PATH

if [ "${BASEDIR:=/}" = "/" ]; then
    BASEDIR=""
else
    BASEDIR_OPT="-b $BASEDIR"
fi

case "$1" in
    configure)
        chown root:bin $BASEDIR/usr/share/man/man4d
	chown root:bin $BASEDIR/usr/share/man/man7
	chown root:bin $BASEDIR/usr/share/man/man8
	chown root:bin $BASEDIR/usr/lib/zones
	chown root:sys $BASEDIR/etc/brand
	chown root:sys $BASEDIR/etc/zones
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/etc/zones/SUNWdefault.xml ] && rm -f $BASEDIR/etc/zones/SUNWdefault.xml
	else
	    chmod 0444 "$BASEDIR/etc/zones/SUNWdefault.xml"
	    chown root:bin "$BASEDIR/etc/zones/SUNWdefault.xml"
	fi
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/etc/zones/SUNWblank.xml ] && rm -f $BASEDIR/etc/zones/SUNWblank.xml
	else
	    chmod 0444 "$BASEDIR/etc/zones/SUNWblank.xml"
	    chown root:bin "$BASEDIR/etc/zones/SUNWblank.xml"
	fi
	chmod 0444 "$BASEDIR/usr/share/lib/xml/dtd/zonecfg.dtd.1"
	chown root:bin "$BASEDIR/usr/share/lib/xml/dtd/zonecfg.dtd.1"
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/lib/svc/method/svc-resource-mgmt ] && rm -f $BASEDIR/lib/svc/method/svc-resource-mgmt
	else
	    chmod 0555 "$BASEDIR/lib/svc/method/svc-resource-mgmt"
	    chown root:bin "$BASEDIR/lib/svc/method/svc-resource-mgmt"
	fi
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/lib/svc/method/svc-zones ] && rm -f $BASEDIR/lib/svc/method/svc-zones
	else
	    chmod 0555 "$BASEDIR/lib/svc/method/svc-zones"
	    chown root:bin "$BASEDIR/lib/svc/method/svc-zones"
	fi
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/lib/svc/method/svc-zonestat ] && rm -f $BASEDIR/lib/svc/method/svc-zonestat
	else
	    chmod 0555 "$BASEDIR/lib/svc/method/svc-zonestat"
	    chown root:bin "$BASEDIR/lib/svc/method/svc-zonestat"
	fi
	chmod 0555 "$BASEDIR/usr/bin/zonestat"
	chown root:bin "$BASEDIR/usr/bin/zonestat"
	chmod 0555 "$BASEDIR/usr/lib/zones/zoneadmd"
	chown root:bin "$BASEDIR/usr/lib/zones/zoneadmd"
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/usr/lib/zones/zonestatd ] && rm -f $BASEDIR/usr/lib/zones/zonestatd
	else
	    chmod 0555 "$BASEDIR/usr/lib/zones/zonestatd"
	    chown root:bin "$BASEDIR/usr/lib/zones/zonestatd"
	fi
	chmod 0555 "$BASEDIR/usr/sbin/zlogin"
	chown root:bin "$BASEDIR/usr/sbin/zlogin"
	chmod 0555 "$BASEDIR/usr/sbin/zoneadm"
	chown root:bin "$BASEDIR/usr/sbin/zoneadm"
	chmod 0555 "$BASEDIR/usr/sbin/zonecfg"
	chown root:bin "$BASEDIR/usr/sbin/zonecfg"
	chmod 0644 "$BASEDIR/usr/share/lib/xml/dtd/brand.dtd.1"
	chown root:bin "$BASEDIR/usr/share/lib/xml/dtd/brand.dtd.1"
	chmod 0644 "$BASEDIR/usr/share/lib/xml/dtd/zone_platform.dtd.1"
	chown root:bin "$BASEDIR/usr/share/lib/xml/dtd/zone_platform.dtd.1"
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/lib/svc/manifest/system/resource-mgmt.xml ] && rm -f $BASEDIR/lib/svc/manifest/system/resource-mgmt.xml
	else
	    chmod 0444 "$BASEDIR/lib/svc/manifest/system/resource-mgmt.xml"
	    chown root:sys "$BASEDIR/lib/svc/manifest/system/resource-mgmt.xml"
	fi
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/lib/svc/manifest/system/zones.xml ] && rm -f $BASEDIR/lib/svc/manifest/system/zones.xml
	else
	    chmod 0444 "$BASEDIR/lib/svc/manifest/system/zones.xml"
	    chown root:sys "$BASEDIR/lib/svc/manifest/system/zones.xml"
	fi
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/lib/svc/manifest/system/zonestat.xml ] && rm -f $BASEDIR/lib/svc/manifest/system/zonestat.xml
	else
	    chmod 0444 "$BASEDIR/lib/svc/manifest/system/zonestat.xml"
	    chown root:sys "$BASEDIR/lib/svc/manifest/system/zonestat.xml"
	fi
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/etc/zones/index ] && rm -f $BASEDIR/etc/zones/index
	else
	    [ -f $BASEDIR/etc/zones/index ] || cp -ax $BASEDIR/usr/share/doc/system-zones/examples/index $BASEDIR/etc/zones/index
	    chmod 0644 "$BASEDIR/etc/zones/index"
	    chown root:sys "$BASEDIR/etc/zones/index"
	fi
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/usr/kernel/drv/amd64/zcons ] && rm -f $BASEDIR/usr/kernel/drv/amd64/zcons
	else
	    chmod 0755 "$BASEDIR/usr/kernel/drv/amd64/zcons"
	    chown root:sys "$BASEDIR/usr/kernel/drv/amd64/zcons"
	fi
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/usr/kernel/drv/amd64/zfd ] && rm -f $BASEDIR/usr/kernel/drv/amd64/zfd
	else
	    chmod 0755 "$BASEDIR/usr/kernel/drv/amd64/zfd"
	    chown root:sys "$BASEDIR/usr/kernel/drv/amd64/zfd"
	fi
	[ "$ZONEINST" = "1" ] || (grep -c "^zcons " $BASEDIR/etc/name_to_major >/dev/null || ( add_drv -n  $BASEDIR_OPT  zcons ) )
	[ "$ZONEINST" = "1" ] || (grep -c "^zfd " $BASEDIR/etc/name_to_major >/dev/null || ( add_drv -n  $BASEDIR_OPT  zfd ) )
	
    ;;

    abort-upgrade|abort-remove|abort-deconfigure)
    ;;

    *)
        echo "postinst called with unknown argument '$1'" >&2
        exit 1
    ;;
esac



# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0

